home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 71 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  94 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: Poor floating point c
  5. X-Nntp-Posting-Host: golden.ripco.com
  6. Message-ID: <DKJKA0.4wG@rci.ripco.com>
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Tue, 2 Jan 1996 06:49:11 GMT
  10.  
  11. >BC++ 4.51 seems to generate very poor floating point code. I would be
  12. >interested to know how well other compilers do on the following
  13. >examples. These were compiled using options: i486 CPU, fast floating
  14. >point, optimize for speed, large memory model.
  15.  
  16. >main()
  17. >       {
  18. >       double x,y,z;
  19. >       int n;
  20.  
  21. >       x = 4.5;
  22. >       y = 10.7;
  23. >       n = x;
  24. >       z = x * y;
  25. >       }
  26.  
  27. [ No comments included about omitting return type or explicit return,
  28.   this time. ]
  29. You're right.  This is horrible optimization.  gcc -O2 recognizes this
  30. code to do nothing, and optimizes it to
  31.     main()
  32.     {
  33.     }
  34.  
  35. If you make x, y, z, and n static, gcc -O2 produces:
  36.  
  37.                             /*        .file   "tst.c"           */
  38.                             /*    gcc2_compiled.:               */
  39.                             /*    ___gnu_compiled_c:            */
  40.                             /*    .lcomm _x.2,8                 */
  41.                             /*    .lcomm _y.3,8                 */
  42.                             /*    .lcomm _z.4,8                 */
  43.                             /*    .lcomm _n.5,4                 */
  44.                             /*    .text                         */
  45.                             /*        .align 2                  */
  46.                             /*    .globl _main                  */
  47. main()
  48.                             /*    _main:                        */
  49.                             /*        pushl %ebp                */
  50.                             /*        movl %esp,%ebp            */
  51.                             /*        subl $8,%esp              */
  52.                             /*        call ___main              */
  53.        {
  54.        static double x,y,z;
  55.        static int n;
  56.        x = 4.5;
  57.                             /*        movl $0,_x.2              */
  58.                             /*        movl $1074921472,_x.2+4   */
  59.        y = 10.7;
  60.                             /*        movl $1717986918,_y.3     */
  61.                             /*        movl $1076192870,_y.3+4   */
  62.        n = x;
  63.                             /*        movl $4,_n.5              */
  64.        z = x * y;
  65.                             /*        movl $858993459,_z.4      */
  66.                             /*        movl $1078465331,_z.4+4   */
  67.        }
  68.                             /*        leave                     */
  69.                             /*        ret                       */
  70.  
  71.  
  72.  
  73.  
  74. >Plain arithmetic, such as z = x * y, is generated inline as one would
  75. >expect. You would think the line n = x would be quite fast. However,
  76. >it generates a function call:
  77.  
  78. >n=x;
  79. >       wait
  80. >       fld qword ptr[bp-0A]
  81. >       call far FTOL@
  82. >       mov [bp-02],ax
  83.  
  84. Check the above.  The compiler can _know_ the result of x * y and move
  85. it directly into z and can _know_ the value to put into n.  There is no
  86. need to generate any arithmetic (inline or otherwise) or any
  87. conversions.
  88.  
  89. [...]
  90.                                     
  91. --
  92. * Martin Ambuhl       net: mambuhl@ripco.com
  93. * Chicago, IL (USA)    
  94.